<!doctype html>
<html>

<head>
  <meta name="generator" content="JSDoc 3.6.11">
  <meta charset="utf-8">
  <title>whatsapp-web.js 1.23.0 &raquo; Source: authStrategies/LegacySessionAuth.js</title>
  <link rel="stylesheet" href="https://brick.a.ssl.fastly.net/Karla:400,400i,700,700i" type="text/css">
  <link rel="stylesheet" href="https://brick.a.ssl.fastly.net/Noto+Serif:400,400i,700,700i" type="text/css">
  <link rel="stylesheet" href="https://brick.a.ssl.fastly.net/Inconsolata:500" type="text/css">
  <link href="css/baseline.css" rel="stylesheet">
</head>

<body onload="prettyPrint()">
  <nav id="jsdoc-navbar" role="navigation" class="jsdoc-navbar">
    <div id="jsdoc-navbar-container">
      <div id="jsdoc-navbar-content">
        <a href="index.html" class="jsdoc-navbar-package-name">whatsapp-web.<wbr>js 1.<wbr>23.<wbr>0</a>
      </div>
    </div>
  </nav>
  <div id="jsdoc-body-container">
    <div id="jsdoc-content">
      <div id="jsdoc-content-container">
        <div id="jsdoc-banner" role="banner">
        </div>
        <div id="jsdoc-main" role="main">
          <header class="page-header">
            <h1>Source: authStrategies/LegacySessionAuth.js</h1>
          </header>
          <article>
            <pre class="prettyprint linenums"><code>&#x27;use strict&#x27;;

const BaseAuthStrategy &#x3D; require(&#x27;./BaseAuthStrategy&#x27;);

/**
 * Legacy session auth strategy
 * Not compatible with multi-device accounts.
 * @param {object} options - options
 * @param {string} options.restartOnAuthFail - Restart client with a new session (i.e. use null &#x27;session&#x27; var) if authentication fails
 * @param {object} options.session - Whatsapp session to restore. If not set, will start a new session
 * @param {string} options.session.WABrowserId
 * @param {string} options.session.WASecretBundle
 * @param {string} options.session.WAToken1
 * @param {string} options.session.WAToken2
 */
class LegacySessionAuth extends BaseAuthStrategy {
    constructor({ session, restartOnAuthFail }&#x3D;{}) {
        super();
        this.session &#x3D; session;
        this.restartOnAuthFail &#x3D; restartOnAuthFail;
    }

    async afterBrowserInitialized() {
        if(this.session) {
            await this.client.pupPage.evaluateOnNewDocument(session &#x3D;&gt; {
                if (document.referrer &#x3D;&#x3D;&#x3D; &#x27;https://whatsapp.com/&#x27;) {
                    localStorage.clear();
                    localStorage.setItem(&#x27;WABrowserId&#x27;, session.WABrowserId);
                    localStorage.setItem(&#x27;WASecretBundle&#x27;, session.WASecretBundle);
                    localStorage.setItem(&#x27;WAToken1&#x27;, session.WAToken1);
                    localStorage.setItem(&#x27;WAToken2&#x27;, session.WAToken2);
                }
  
                localStorage.setItem(&#x27;remember-me&#x27;, &#x27;true&#x27;);
            }, this.session);
        }
    }

    async onAuthenticationNeeded() {
        if(this.session) {
            this.session &#x3D; null;
            return {
                failed: true,
                restart: this.restartOnAuthFail,
                failureEventPayload: &#x27;Unable to log in. Are the session details valid?&#x27;
            };
        }

        return { failed: false };
    }

    async getAuthEventPayload() {
        const isMD &#x3D; await this.client.pupPage.evaluate(() &#x3D;&gt; {
            return window.Store.MDBackend;
        });

        if(isMD) throw new Error(&#x27;Authenticating via JSON session is not supported for MultiDevice-enabled WhatsApp accounts.&#x27;);

        const localStorage &#x3D; JSON.parse(await this.client.pupPage.evaluate(() &#x3D;&gt; {
            return JSON.stringify(window.localStorage);
        }));

        return {
            WABrowserId: localStorage.WABrowserId,
            WASecretBundle: localStorage.WASecretBundle,
            WAToken1: localStorage.WAToken1,
            WAToken2: localStorage.WAToken2
        };
    }
}

module.exports &#x3D; LegacySessionAuth;
</code></pre>
          </article>
        </div>
      </div>
      <nav id="jsdoc-toc-nav" role="navigation"></nav>
    </div>
  </div>
  <footer id="jsdoc-footer" class="jsdoc-footer">
    <div id="jsdoc-footer-container">
      <p>
        Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc</a> 3.6.11 on October 21, 2023.
      </p>
    </div>
  </footer>
  <script src="scripts/jquery.min.js"></script>
  <script src="scripts/tree.jquery.js"></script>
  <script src="scripts/prettify.js"></script>
  <script src="scripts/jsdoc-toc.js"></script>
  <script src="scripts/linenumber.js"></script>
  <script src="scripts/scrollanchor.js"></script>
</body>

</html>